--[[----------------------------------------------------------------------------
    This file is part of yalps.

    yalps is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    yalps is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with yalps.  If not, see <http://www.gnu.org/licenses/>.
------------------------------------------------------------------------------]]
--[[----------------------------------------------------------------------------
    Defines functions for running commands
------------------------------------------------------------------------------]]

local LrTasks = import 'LrTasks'
local LrLogger = import 'LrLogger'
local LrPathUtils = import 'LrPathUtils'

local myLogger = LrLogger('yalps-logger')
myLogger:enable("logfile")

CommandRunner = {}

-- Executes a command with parameter substitution
function CommandRunner.run(command, parameters)
	
	local realCommand = command
	myLogger:trace("Command before param substitution: " .. realCommand)
	for k,v in pairs(parameters) do
		myLogger:trace("Replacing " .. k .. " with " .. v)
		realCommand = string.gsub(realCommand, k, v)
		myLogger:trace("Replaced " .. realCommand)
	end
	-- Standardize the path
	realCommand = LrPathUtils.standardizePath(realCommand)
	myLogger:trace("Command to be executed: " .. realCommand)
	-- Todo add check to know if want to execute sync o async
	--LrTasks.startAsyncTask(function()
		LrTasks.execute(realCommand)
	--end, "Run command")
end